import { isEmail } from '@utils/regular.js'

/* 昵称 */
export const nickname = [
  { required: true, message: '请输入昵称', trigger: 'blur' },
  { min: 2, max: 20, message: '长度在2到20个字符', trigger: 'blur' }
]
/* 性别 */
export const sex = [
  { required: true, message: '请选择性别', trigger: 'change' }
]
/* 邮箱 */
const checkEmail = (_rule, value, callback) => {
  if (!isEmail(value)) {
    callback(new Error('请输入正确的邮箱地址'))
  }
  callback()
}
export const email = [
  { required: true, message: '请输入邮箱地址', trigger: 'blur' },
  { validator: checkEmail, trigger: 'blur' }
]
/* 验证码 */
export const captcha = [
  { required: true, message: '请输入验证码', trigger: 'blur' }
]
// xz
export const account = [
  { required: true, message: '账号不能为空', trigger: 'blur' },
  { min: 4, max: 20, message: '长度在4到20个字符', trigger: 'blur' }
]

export const password = [
  { required: true, message: '密码不能为空', trigger: 'blur' },
  { min: 8, message: '密码至少8位', trigger: 'blur' }
]
export const confirmPassword = (formRef) => [
  { 
    validator: (rule, value, callback) => {
      if (!value || value !== formRef?.password) {
        callback(new Error('必须与密码保持一致'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  }
]